﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace MyWordCount
{
    /// <summary>
    /// 指令操作
    /// </summary>
    class ProcessingData
    {
        /// <summary>
        /// 统计文件中的字符个数
        /// </summary>
        /// <param name="fstr">要统计的文件名</param>
        /// <returns>返回字符数</returns>
        public int cProsess(string fstr)
        {
            string str = openFile(fstr);
            int charCount = 0;//记录字符个数
            foreach (char c in str)
            {
                if (c == ' ' || c == '\t' || c == '\n')
                {
                    charCount++;
                }
            }
            StreamWriter sw = new StreamWriter("result.txt", true);
            sw.WriteLine(fstr + "  字符数： " + Convert.ToString(charCount));
            sw.Close();
            return charCount;
        }

        /// <summary>
        /// 统计文件中的单词数
        /// </summary>
        /// <param name="wstr">要统计的文件名</param>
        /// <returns>返回单词数</returns>
        public int wProsess(string wstr)
        {
            string str = openFile(wstr);
            int count = 0;//记录单词个数
            bool isblank = true;
            foreach (char s in str)//遍历字符串，
            {
                if (s != ' ' && s != '\n' && isblank == true)
                {
                    count++;
                    isblank = false;
                }
                else if ((s == ' ' || s == '\n') && isblank == false)
                {
                    isblank = true;
                }
            }
            StreamWriter sw = new StreamWriter("result.txt", true);
            sw.WriteLine(wstr + "  单词数：" + Convert.ToString(count - 1));//
            sw.Close();
            return count - 1;
        }

        /// <summary>
        /// 统计文件中的行数
        /// </summary>
        /// <param name="lstr">要统计的文件名</param>
        /// <returns>返回行数</returns>
        public int lProsess(string lstr)
        {
            string str = openFile(lstr);
            int count = 0;//记录行数
            foreach (char s in str)
            {
                if (s == '\n')
                {
                    count++;
                }
            }
            StreamWriter sw = new StreamWriter("result.txt", true);
            sw.WriteLine(lstr + "   行数： " + Convert.ToString(count - 1));
            sw.Close();
            return count - 1;
        }

        /// <summary>
        /// 输出结果到指定文件
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool oProcess(string oFile)
        {
            string str = openFile("result.txt");//打开结果保存文件

            StreamWriter sw = new StreamWriter(oFile, true);
            sw.WriteLine(str);
            sw.Close();// 关于写入文件的操作  http://www.cnblogs.com/duanjt/p/5265655.html
            return true;
            //---------------------

            //本文来自 lr_旅人 的CSDN 博客 ，全文地址请点击：https://blog.csdn.net/u013569749/article/details/56494880?utm_source=copy 

        }

        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="fstr">文件名</param>
        /// <returns>返回字符串</returns>
        public string openFile(string fstr)
        {

            FileStream fs = new FileStream(@fstr, FileMode.Open);//初始化文件流
            byte[] array = new byte[fs.Length];//初始化字节数组
            fs.Read(array, 0, array.Length);//读取流中数据到字节数组中
            fs.Close();//关闭流
            string str = Encoding.UTF8.GetString(array);
            return str;

        }
    }
}
